import router from '@/router'
import * as auth from 'utils/auth'
import store from '@/store'
let whiteRouter = ['login', 'phonelogin', 'register', 'qrcodelogin', 'sociallogin']
router.beforeEach((to, from, next) => {
  if (whiteRouter.indexOf(to.name) > 0) {
    next()
  } else {
    if (!auth.getCookie()) {
      if (to.name !== 'login') {
        next({
          name: 'login'
        })
      } else {
        next()
      }
    } else {
      if (!store.state.account.userInfo.userToken) {
        store.state.account.userInfo.userToken = auth.getCookie()
        store.dispatch('_getUserInfo', {token: store.state.account.userInfo.userToken})
          .then(() => {
            next({...to, replace: true})
          })
          .catch((err) => {
            console.log(err)
          })
      } else {
        next()
      }
    }
  }
})
